$(function() {
    function loadData() {
        this.data = {
            seaName: '',
            arrays:[],
            arrayName:[],
            sub_index:1,
            subid:0
        };
        this.init();
    }

    loadData.prototype.init = function() {
        var _this = this.data;
        //封装地址搜索
        eflower_GdMap = {
            GdSearchload: function() {
                var Lbody = document.body;
                var LO = document.createElement("div");
                LO.style.display = 'none';
                LO.setAttribute('id', 'MarkMap');
                Lbody.appendChild(LO);
                var sce = document.createElement('script')
                sce.setAttribute('type', 'text/javascript');
                sce.setAttribute('src', 'http://cache.amap.com/lbs/static/PlaceSearchRender.js');
                Lbody.appendChild(sce);
            },
            GdSearch: function(val, idmode) {
                var map = new AMap.Map("MarkMap");
                var placeSearchOptions = { //构造地点查询类
                    pageSize: 100,
                    pageIndex: 1
                };
                var placeSearch = new AMap.PlaceSearch(placeSearchOptions);
                placeSearch.search(val, callback);
                var placeSearchRender = new Lib.AMap.PlaceSearchRender();

                function callback(status, result) {
                    if (status === 'complete' && result.info === 'OK') {
                        var data = result.poiList.pois
                        var Oul = document.createElement('ul');
                        Oul.style.listStyle = "none";
                        // Oul.style.margin = "10px 0px 10px 10px";
                        Oul.style.padding = "0px";
                        for (var i = 0; i < data.length; i++) {
                            if (data[i].type != '地名地址信息;普通地名;地市级地名' && data[i].type != '地名地址信息;普通地名;省级地名' && data[i].type != '地名地址信息;普通地名;区县级地名') {
                                var Li = document.createElement("li");
                                Li.style.width = '100%';
                                // Li.style.height = '35px';
                                // Li.style.paddingLeft = '10px';
                                // Li.style.lineHeight = '35px';
                                // Li.style.borderBottom = '1px solid #e5e5e5';
                                Li.setAttribute('lg', data[i].location.lng + ',' + data[i].location.lat);
                                Li.innerHTML = data[i].name;
                                Oul.appendChild(Li);
                            }
                        };
                        idmode[0].innerHTML = "";
                        idmode[0].appendChild(Oul);
                    } else {
                        $.toast("查无数据！！");
                    }
                }

            }
        }

        //初始化请求
        eflower_GdMap.GdSearchload();

        //搜索获取地址
        $(document).on('click', '#butseach', function() {
            var intext = app.Trim($('#search').val(), 'g');
            if (intext == '') {
                $.toast("请输入搜索信息");
                return false;
            } else if (_this.seaName != intext) {
                _this.seaName = intext;
                eflower_GdMap.GdSearch(intext, $('#GdList'));
            }
        });

        //输入框改变情况列表数据
        $(document).on('change', '#search', function() {
            $('#GdList').html('');
        });

        //获取搜索地址 ip
        
        $(document).on('click', '#GdList li', function() {
            var arraysd=$(this).attr('lg').split(',');
            if(_this.arrays.length>0){
	            for(var i=0; i<_this.arrays.length; i++){
	            	if(_this.arrays[i].position[0]==arraysd[0] && _this.arrays[i].position[1]==arraysd[1]){
	            		$.toast("已加入搜索列表");
	            		return false;
	            	}
	            }
            }
            var data = {
                icon: 'http://webapi.amap.com/theme/v1.3/markers/n/mark_b' +  _this.sub_index + '.png',
                name:$(this).html(),
                id:_this.subid,
                position: arraysd
            }
            
            _this.subid++;
            //改变样式
            $(this).css('color','#f87f4a')


            _this.arrays.push(data)
            _this.arrayName.push($(this).html())
            _this.sub_index++;
        })

        //页面跳转
        $(document).on('click', '#GoMark,#gomarkbtn', function() {
            console.log(_this.arrays)
            localStorage.setItem('mark', JSON.stringify(_this.arrays))
            window.location.href = "Maker.html"
        })

        //打开下拉盒子
        $(document).on('click','.boxfall',function(){
        	$('.popupself').show()
        	setTimeout(function() {
	        	$('.popupself').css({
	        		  "transition": "opacity .5s",
			          "-webkit-transition": "opacity .5s",
			          "-moz-transition": "opacity .5s",
			          "-o-transition": "opacity .5s",
			          "-ms-transition": "opacity .5s",
			          "opacity": "1"
	        	})
        	}, 100);
        	var html=''
        	if(_this.arrayName.length>0){
        		$.each(_this.arrayName,function(i,v){
        			html+=` <div class="col-50 textone">
                			${v}</div>`;
        		})
        		$('#gomarkbtn').show();
        	}else{
        		html='<div class="nodata">还未添加数据！</div>'
        		$('#gomarkbtn').hide();
        	}
        	$('.Namelist').html(html);

        })

        //关闭盒子
        $(document).on('click','.modelop',function(){
        	$('.popupself').css("opacity","0")
        	setTimeout(function() {
	        	$('.popupself').hide()
        	}, 500);
        })

        //禁止标记
        $(document).on('click','.Namelist .textone',function(event){
        	if($(this).hasClass('none')){
        		$(this).removeClass('none');
        	}else{
        		$(this).addClass('none');
        	}
        })










    };

    new loadData();


})